home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / app / palette_select.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-25  |  7.6 KB  |  306 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  * Copyright (C) 1998 Andy Thomas (alt@picnic.demon.co.uk)
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include <string.h>
  23.  
  24. #include <glib.h>
  25.  
  26. #include "apptypes.h"
  27. #include "gimpui.h"
  28. #include "palette_entries.h"
  29. #include "palette_select.h"
  30. #include "paletteP.h"
  31.  
  32. #include "libgimp/gimpintl.h"
  33.  
  34. /*  List of active dialogs  */
  35. static GSList *active_dialogs = NULL;
  36.  
  37. /*  local function prototypes  */
  38. static gint   palette_select_button_press   (GtkWidget *, GdkEventButton *, gpointer);
  39. static void   palette_select_close_callback (GtkWidget *, gpointer);
  40. static void   palette_select_edit_callback  (GtkWidget *, gpointer);
  41.  
  42. /*  public functions  */
  43.  
  44. PaletteSelect *
  45. palette_select_new (gchar *title,
  46.             gchar *initial_palette)
  47. {
  48.   PaletteEntries *p_entries = NULL;
  49.   PaletteSelect  *psp;
  50.   GSList     *list;
  51.   GtkWidget  *vbox;
  52.   GtkWidget  *hbox;
  53.   GtkWidget  *scrolled_win;
  54.   gchar      *titles[3];
  55.   gint        select_pos;
  56.  
  57.   palette_select_palette_init ();
  58.  
  59.   psp = g_new (PaletteSelect, 1);
  60.   psp->callback_name = NULL;
  61.   
  62.   /*  The shell and main vbox  */
  63.   psp->shell = gimp_dialog_new (title ? title : _("Palette Selection"),
  64.                 "palette_selection",
  65.                 gimp_standard_help_func,
  66.                 "dialogs/palette_selection.html",
  67.                 GTK_WIN_POS_MOUSE,
  68.                 FALSE, TRUE, FALSE,
  69.  
  70.                 _("Edit"), palette_select_edit_callback,
  71.                 psp, NULL, NULL, TRUE, FALSE,
  72.                 _("Close"), palette_select_close_callback,
  73.                 psp, NULL, NULL, FALSE, TRUE,
  74.  
  75.                 NULL);
  76.  
  77.   vbox = gtk_vbox_new (FALSE, 1);
  78.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 1);
  79.   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (psp->shell)->vbox), vbox);
  80.  
  81.   hbox = gtk_hbox_new (FALSE, 8);
  82.   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  83.   gtk_widget_show (hbox);
  84.  
  85.   /* clist preview of gradients */
  86.   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  87.   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
  88.                   GTK_POLICY_AUTOMATIC,
  89.                   GTK_POLICY_ALWAYS);
  90.   gtk_box_pack_start (GTK_BOX (hbox), scrolled_win, TRUE, TRUE, 0); 
  91.   gtk_widget_show (scrolled_win);
  92.  
  93.   titles[0] = _("Palette");
  94.   titles[1] = _("Ncols");
  95.   titles[2] = _("Name");
  96.   psp->clist = gtk_clist_new_with_titles (3, titles);
  97.   gtk_clist_set_shadow_type (GTK_CLIST (psp->clist), GTK_SHADOW_IN);
  98.   gtk_clist_set_row_height (GTK_CLIST (psp->clist), SM_PREVIEW_HEIGHT + 2);
  99.   gtk_clist_set_use_drag_icons (GTK_CLIST (psp->clist), FALSE);
  100.   gtk_clist_column_titles_passive (GTK_CLIST (psp->clist));
  101.   gtk_widget_set_usize (psp->clist, 203, 200);
  102.   gtk_clist_set_column_width (GTK_CLIST (psp->clist), 0, SM_PREVIEW_WIDTH + 2);
  103.   gtk_container_add (GTK_CONTAINER (scrolled_win), psp->clist);
  104.   gtk_widget_show (psp->clist);
  105.  
  106.   select_pos = -1;
  107.   if (initial_palette && strlen (initial_palette))
  108.     {
  109.       for (list = palette_entries_list; list; list = g_slist_next (list))
  110.     {
  111.       p_entries = (PaletteEntries *) list->data;
  112.       
  113.       if (strcmp (p_entries->name, initial_palette) > 0)
  114.         break;
  115.  
  116.       select_pos++;
  117.     }
  118.     }
  119.  
  120.   gtk_widget_realize (psp->shell);
  121.   psp->gc = gdk_gc_new (psp->shell->window);  
  122.   
  123.   palette_clist_init (psp->clist, psp->shell, psp->gc);
  124.   gtk_signal_connect (GTK_OBJECT (psp->clist), "button_press_event",
  125.               GTK_SIGNAL_FUNC (palette_select_button_press),
  126.               (gpointer) psp);
  127.  
  128.   /* Now show the dialog */
  129.   gtk_widget_show (vbox);
  130.   gtk_widget_show (psp->shell);
  131.  
  132.   if (select_pos != -1) 
  133.     {
  134.       gtk_clist_select_row (GTK_CLIST (psp->clist), select_pos, -1);
  135.       gtk_clist_moveto (GTK_CLIST (psp->clist), select_pos, 0, 0.0, 0.0); 
  136.     }
  137.   else
  138.     gtk_clist_select_row (GTK_CLIST (psp->clist), 0, -1);
  139.  
  140.   active_dialogs = g_slist_append (active_dialogs, psp);
  141.  
  142.   return psp;
  143. }
  144.  
  145. void
  146. palette_select_clist_insert_all (PaletteEntries *p_entries)
  147. {
  148.   PaletteEntries *chk_entries;
  149.   PaletteSelect *psp; 
  150.   GSList *list;
  151.   gint pos = 0;
  152.  
  153.   for (list = palette_entries_list; list; list = g_slist_next (list))
  154.     {
  155.       chk_entries = (PaletteEntries *) list->data;
  156.       
  157.       /*  to make sure we get something!  */
  158.       if (chk_entries == NULL)
  159.     return;
  160.  
  161.       if (strcmp (p_entries->name, chk_entries->name) == 0)
  162.     break;
  163.  
  164.       pos++;
  165.     }
  166.  
  167.   for (list = active_dialogs; list; list = g_slist_next (list))
  168.     {
  169.       psp = (PaletteSelect *) list->data;
  170.  
  171.       gtk_clist_freeze (GTK_CLIST (psp->clist));
  172.       palette_clist_insert (psp->clist, psp->shell, psp->gc, p_entries, pos);
  173.       gtk_clist_thaw (GTK_CLIST (psp->clist));
  174.     }
  175. }
  176.  
  177. void
  178. palette_select_set_text_all (PaletteEntries *entries)
  179. {
  180.   PaletteEntries *p_entries = NULL;
  181.   PaletteSelect *psp; 
  182.   GSList *list;
  183.   gchar *num_buf;
  184.   gint pos = 0;
  185.  
  186.   for (list = palette_entries_list; list;  list = g_slist_next (list))
  187.     {
  188.       p_entries = (PaletteEntries *) list->data;
  189.       
  190.       if (p_entries == entries)
  191.     break;
  192.  
  193.       pos++;
  194.     }
  195.  
  196.   if (p_entries == NULL)
  197.     return; /* This is actually an error */
  198.  
  199.   num_buf = g_strdup_printf ("%d",p_entries->n_colors);;
  200.  
  201.   for (list = active_dialogs; list; list = g_slist_next (list))
  202.     {
  203.       psp = (PaletteSelect *) list->data;
  204.  
  205.       gtk_clist_set_text (GTK_CLIST (psp->clist), pos, 1, num_buf);
  206.     }
  207.  
  208.   g_free (num_buf);
  209. }
  210.  
  211. void
  212. palette_select_refresh_all ()
  213. {
  214.   PaletteSelect *psp; 
  215.   GSList *list;
  216.  
  217.   for (list = active_dialogs; list; list = g_slist_next (list))
  218.     {
  219.       psp = (PaletteSelect *) list->data;
  220.  
  221.       gtk_clist_freeze (GTK_CLIST (psp->clist));
  222.       gtk_clist_clear (GTK_CLIST (psp->clist));
  223.       palette_clist_init (psp->clist, psp->shell, psp->gc);
  224.       gtk_clist_thaw (GTK_CLIST (psp->clist));
  225.     }
  226. }
  227.  
  228. /*  local functions  */
  229.  
  230. static gint
  231. palette_select_button_press (GtkWidget      *widget,
  232.                  GdkEventButton *bevent,
  233.                  gpointer        data)
  234. {
  235.   PaletteSelect *psp;
  236.  
  237.   psp = (PaletteSelect *) data;
  238.  
  239.   if (bevent->button == 1 && bevent->type == GDK_2BUTTON_PRESS)
  240.     {
  241.       palette_select_edit_callback (widget, data);
  242.  
  243.       return TRUE;
  244.     }
  245.  
  246.   return FALSE;
  247. }
  248.  
  249. static void
  250. palette_select_edit_callback (GtkWidget *widget,
  251.                   gpointer   data)
  252. {
  253.   PaletteEntries *p_entries = NULL;
  254.   PaletteSelect *psp = (PaletteSelect *) data;
  255.   GList *sel_list;
  256.  
  257.   sel_list = GTK_CLIST (psp->clist)->selection;
  258.  
  259.   while (sel_list)
  260.     {
  261.       gint row;
  262.  
  263.       row = GPOINTER_TO_INT (sel_list->data);
  264.  
  265.       p_entries = 
  266.     (PaletteEntries *) gtk_clist_get_row_data (GTK_CLIST (psp->clist), row);
  267.  
  268.       palette_create_edit (p_entries);
  269.  
  270.       /* One only */
  271.       return;
  272.     }
  273. }
  274.  
  275. static void
  276. palette_select_free (PaletteSelect *psp)
  277. {
  278.   if (psp)
  279.     {
  280.       /*
  281.       if(psp->callback_name)
  282.     g_free (gsp->callback_name);
  283.       */
  284.  
  285.       /* remove from active list */
  286.       active_dialogs = g_slist_remove (active_dialogs, psp); 
  287.  
  288.       g_free (psp);
  289.     }
  290. }
  291.  
  292. static void
  293. palette_select_close_callback (GtkWidget *widget,
  294.                    gpointer   data)
  295. {
  296.   PaletteSelect *psp;
  297.  
  298.   psp = (PaletteSelect *) data;
  299.  
  300.   if (GTK_WIDGET_VISIBLE (psp->shell))
  301.     gtk_widget_hide (psp->shell);
  302.  
  303.   gtk_widget_destroy (psp->shell); 
  304.   palette_select_free (psp); 
  305. }
  306.